package com.appcenter.jz.filter;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2019/3/17.
 */
@Service
public class SooUserService implements UserDetailsService{

	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		UserDetails userDetails = null;
		try {
/*			com.favccxx.favsecurity.pojo.User favUser = new com.favccxx.favsecurity.pojo.User();
			favUser.setUsername("favccxx");
			favUser.setPassword("favccxx");*/
			Collection<GrantedAuthority> authList = getAuthorities();
			userDetails = new User(username, "password",true,true,true,true,authList);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return userDetails;
	}


	/**  * 获取用户的角色权限,为了降低实验的难度，这里去掉了根据用户名获取角色的步骤     * @param    * @return   */
	private Collection<GrantedAuthority> getAuthorities(){
		List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
		authList.add(new SimpleGrantedAuthority("ROLE_USER"));
		authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		return authList;
	}

}
